//
//  YDBBannerOverLayout.swift
//  BaseFramework
//
//  Created by 杨东宝 on 2020/5/9.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class YDBBannerOverLayout: UICollectionViewFlowLayout {
    
    var param:YDBBannerParam!
    
    
    lazy var collectionContenOffset: CGPoint = {
        
        return CGPoint(x:Int(self.collectionView!.contentOffset.x), y:Int(self.collectionView!.contentOffset.y))
        
    }()
    
    
   lazy var collectionContenSize:CGSize = {
   
    return CGSize(width:Int(self.collectionView!.bounds.size.width), height:Int(self.collectionView!.bounds.size.height))
        
    }()
 
    override init() {
        super.init()
    }
    convenience init(param:YDBBannerParam) {
        self.init()
        self.param = param
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepare() {
        super.prepare()
        if (self.param!.wCardOverLapCount<=0) {
            self.param!.wCardOverLapCount = 4;
        }
        
        self.param!.wItemSize = param!.wItemSize ?? CGSize(width: self.collectionView!.frame.size.width/2, height:  self.collectionView!.frame.size.height)
        
        self.itemSize = self.param!.wVertical ? CGSize(width: self.param!.wItemSize!.width, height:  (self.param.wItemSize!.height - (CGFloat(self.param!.wCardOverLapCount - 1))*self.param!.wLineSpacing)) :
            CGSize(width: self.param!.wItemSize!.width - CGFloat(self.param!.wCardOverLapCount - 1)*self.param!.wLineSpacing, height:self.param!.wItemSize!.height)
        
        
        
        self.minimumInteritemSpacing = (self.param!.wFrame!.size.height - self.param!.wItemSize!.height)/2
        self.minimumLineSpacing = self.param!.wLineSpacing
        self.sectionInset = self.param!.wSectionInset;
        if self.collectionView!.isPagingEnabled {
            self.scrollDirection = self.param!.wVertical ? .vertical : .horizontal
        }else{
            self.scrollDirection = .horizontal
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        self.cardOverLapTypeInRect(rect: rect)
        
    }
    
    //卡片重叠
    func cardOverLapTypeInRect(rect:CGRect) -> [UICollectionViewLayoutAttributes] {
        
        let itemsCount:Int  = self.collectionView!.numberOfItems(inSection: 0);
        if (itemsCount <= 0) {
            return [];
        }
        
        self.param!.myCurrentPath = self.param!.wVertical ? Int(max(floor(self.collectionContenOffset.y / (self.collectionContenSize.height)), 0)) : Int(max(floor(self.collectionContenOffset.x / (self.collectionContenSize.width)), 0))
        
        var minVisibleIndex:Int = Int(max(self.param.myCurrentPath,0))
        
        var contentOffset : Int  =  Int(self.param.wVertical ?
            self.collectionContenOffset.y:self.collectionContenOffset.x);

        var collectionBounds :Int  = Int(self.param.wVertical ?
            self.collectionContenSize.height : self.collectionContenSize.width);
        
        var offset:CGFloat  = CGFloat(contentOffset % collectionBounds);
        
        var offsetProgress:  CGFloat  = offset / (self.param.wVertical ? self.collectionContenSize.height:self.collectionContenSize.width)*1.0;
        
        var maxVisibleIndex: NSInteger  = max(min(itemsCount - 1, self.param.myCurrentPath + self.param.wCardOverLapCount), minVisibleIndex);
        
        var mArr:NSArray = []
        
        for i in minVisibleIndex...maxVisibleIndex {

        var index:IndexPath  = IndexPath(row: i, section: 0)
            
        var attributes: UICollectionViewLayoutAttributes = self.
            
            
            
            
            
        }
        
        
        
    }
    
    func getCopyOfAttributes(attributes:Array<UICollectionViewLayoutAttributes>) -> Array<UICollectionViewLayoutAttributes> {
        
        var copyArr = Array<UICollectionViewLayoutAttributes>()
        
        for attr in attributes {
            copyArr.append(attr)
        }
        return copyArr;
        
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
      return true
    }
    
    func parallaxProgress(visibleIndex:Int,offsetProgress:CGFloat,minScale:CGFloat) -> CGFloat {
           
        let step: CGFloat  = (1.0 - minScale) / CGFloat((self.param!.wCardOverLapCount - 1))*1.0
        
        return (1.0 - CGFloat(visibleIndex - 1)) * step + (step * offsetProgress)
        
     }
}
